package com.example.demo.controller;


import com.example.demo.entity.Patent;
import com.example.demo.entity.User;
import com.example.demo.entity.dto.ResultBean;
import com.example.demo.exception.bizException.BizException;
import com.example.demo.exception.bizException.BizExceptionCodeEnum;
import com.example.demo.service.PatentService;
import com.example.demo.util.Role;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cy
 * @since 2020-10-09
 * @version v1.0
 */
@RestController
@RequestMapping("/patent")
public class PatentController {

    @Autowired
    private PatentService patentService;

    /**
     * 查询分页数据
     */
    @RequestMapping(method = RequestMethod.GET,value = "/normal")
    public ResultBean<?> listByPage(@RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                    @RequestParam(name = "userId", defaultValue = "",required = false) Integer userId,
                                    @RequestParam(name = "achieveName", defaultValue = "") String  achieveName,
                                    @RequestParam(name = "patentType", defaultValue = "") String  patentType,
                                    @RequestParam(name = "year", defaultValue = "") String  year,
                                    @RequestParam(name = "state", defaultValue = "") Integer  state) {
        return new ResultBean<>(patentService.listAchievesByPage(page, pageSize,userId,achieveName,patentType,year,state));
    }


    /**
    * 根据id查询
    */
    @RequestMapping(method = RequestMethod.GET, value = "/normal/{id}")
    public ResultBean<?> getById(@PathVariable("id") Integer id) {
        return new ResultBean<>(patentService.getPatentById(id));
    }

    /**
    * 新增
    */
    @RequestMapping(method = RequestMethod.POST)
    @RequiresRoles({Role.TEAM})
    public ResultBean<?> insert(@RequestBody Patent patent) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        patent.setUId(user.getUserId());
        return new ResultBean<>(patentService.insertPatent(patent));
    }

    /**
    * 删除
    */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    @RequiresRoles({Role.TEAM})
    public ResultBean<?> deleteById(@PathVariable("id") Integer id) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        return new ResultBean<>(patentService.deletePatentById(id,user.getUserId()));
    }

    /**
    * 修改
    */
    @RequestMapping(method = RequestMethod.PUT)
    @RequiresRoles({Role.TEAM})
    public ResultBean<?> updateById(@RequestBody Patent patent) {
        return new ResultBean<>(patentService.updatePatent(patent));
    }


    @RequiresRoles(Role.ADMIN)
    @RequestMapping(method = RequestMethod.PUT, value = "/check")
    public ResultBean<?> checkDemand(@RequestBody Patent Patent) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        return new ResultBean<>(patentService.checkPatent(Patent));
    }

    /**
     * 文件导入专利
     */
    @RequestMapping(method = RequestMethod.POST,value = "/importPatent")
    public ResultBean<?> importPatent(@RequestParam("patent") MultipartFile file) throws IOException {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        String filename = file.getOriginalFilename();
        return new ResultBean<>(patentService.importPatent(filename,file,user));
    }

    /**
     *  查询未审核的专利数量
     */
    @RequestMapping(method = RequestMethod.GET,value = "/uncheckedNum")
    public  ResultBean<?> UncheckedNum(){
        return new ResultBean<>(patentService.UncheckedNum());
    }
}
